package com.app.manage.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

public class SplitUtils {


    public static <T,R> List<R> splitDeal(Integer oneCounts, List<T> dataList,
                                          Function<List<T>, List<R>> function){
        List<R> rList = new ArrayList<>();
        int splitCounts = dataList.size()/oneCounts + (dataList.size() %oneCounts > 0 ?1:0);
        for(int i = 0 ; i < splitCounts ; i++){
            List<T> tempList = new ArrayList<>();
            if(i < splitCounts -1){
                tempList.addAll(dataList.subList(i * oneCounts ,i*oneCounts +oneCounts));
            }else {
                tempList.addAll(dataList.subList(i * oneCounts,dataList.size()));
            }
            rList.addAll(function.apply(tempList));
        }
        return rList;
    }


    public static <T>  void  splitDeal(Integer oneCounts, List<T> dataList,
                                       Consumer<? super List<T>> consumer){
        int splitCounts = dataList.size()/oneCounts + (dataList.size() %oneCounts > 0 ?1:0);
        for(int i = 0 ; i < splitCounts ; i++){
            List<T> tempList = new ArrayList<>();
            if(i < splitCounts -1){
                tempList.addAll(dataList.subList(i * oneCounts ,i*oneCounts +oneCounts));
            }else {
                tempList.addAll(dataList.subList(i * oneCounts,dataList.size()));
            }
            consumer.accept(tempList);
        }
    }
}
